package com.wu.leetcode.leetcode48;

/**
 * Leetcode48-旋转图像
 *
 * @author lynn
 * @date 2020/11/1 - 11:05
 */
public class Solution {
    public static int[][] rotate(int[][] matrix) {
        if (matrix.length == 0 || matrix.length != matrix[0].length) {
            return matrix;
        }
        int len = matrix.length;
        for (int i = 0; i < len / 2; i++) {
            for (int j = i; j < len - 1 - i; j++) {
                int temp = matrix[i][j];
                matrix[i][j] = matrix[len - 1 - j][i];
                matrix[len - 1 - j][i] = matrix[len - 1 - i][len - 1 - j];
                matrix[len - 1- i][len - 1 - j] = matrix[j][len - 1 - i];
                matrix[j][len - 1 - i] = temp;
            }
        }
        return matrix;
    }

    public static void main(String[] args) {
       int[][] matrix =  new int[][]{
               {1,2,3,4},
               {5,6,7,8},
               {9,10,11,12},
               {13,14,15,16}
        };

        int[][] rotate = rotate(matrix);
        for (int[] ints : rotate) {
            for (int anInt : ints) {
                System.out.print(anInt + " ");
            }
            System.out.println();
        }
    }
}